local yuejian = fk.CreateSkill {

  name = "joy__yuejian",

  tags = { Skill.Compulsory, },

}



yuejian:addEffect("viewas", {
  name = "joy__yuejian",
  pattern = ".|.|.|.|.|basic",
  prompt = "#joy__yuejian",
  interaction = function(self,player)
    local names = {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if card.type == Card.TypeBasic and not card.is_derived then
        local to_use = Fk:cloneCard(card.name)
        if ((Fk.currentResponsePattern == nil and player:canUse(to_use) and not player:prohibitUse(to_use)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
          table.insertIfNeed(names, card.name)
        end
      end
    end
    return UI.ComboBox { choices = names }
  end,
  card_filter = function(self, player, to_select, selected)
    return false
  end,
  view_as = function(self, player,cards)
    if not self.interaction.data then return nil end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = yuejian.name
    return card
  end,
  enabled_at_play = function(self, player)
    return player:getMark("joy__yuejian-turn") == 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and player:getMark("joy__yuejian-turn") == 0
  end,
})

yuejian:addEffect(fk.AfterCardUseDeclared, {
  refresh_events = {fk.AfterCardUseDeclared, fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardUseDeclared then
      return target == player and data.card.type == Card.TypeBasic and player:getMark("joy__yuejian-turn") == 0
    elseif target == player and data == self and player:getMark("joy__yuejian-turn") == 0 then
      return #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data[1]
        return use.from == player.id and use.card.type == Card.TypeBasic
      end, Player.HistoryRound) > 0
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "joy__yuejian-turn", 1)
  end,
})
yuejian:addEffect(fk.EventAcquireSkill, {
  refresh_events = {fk.AfterCardUseDeclared, fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardUseDeclared then
      return target == player and data.card.type == Card.TypeBasic and player:getMark("joy__yuejian-turn") == 0
    elseif target == player and data == self and player:getMark("joy__yuejian-turn") == 0 then
      return #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data[1]
        return use.from == player.id and use.card.type == Card.TypeBasic
      end, Player.HistoryRound) > 0
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "joy__yuejian-turn", 1)
  end,
})

yuejian:addEffect("maxcards", {
  name = "#joy__yuejian_maxcards",
  frequency = Skill.Compulsory,
  correct_func = function(self, player)
    if player:hasSkill("joy__yuejian") then
      return player.maxHp
    else
      return 0
    end
  end,
})

return yuejian